package com.huan.service.impl;

import com.huan.dao.mapper.CountMapper;
import com.huan.pojo.Count;
import com.huan.pojo.PageBean;
import com.huan.service.CountService;
import com.huan.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class CountServiceImpl implements CountService {
    //1.创建工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //根据生源地统计学生人数
    @Override
    public PageBean<Count> selectCountPlace(int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        //计算开始索引
        int begin = (currentPage - 1)*pageSize;
        //计算查询条目数
        int size = pageSize;

        //查询当前页数据
        List<Count> rows = mapper.selectCountPlace(begin, size);

        //查询总记录数
        int totalCount = mapper.selectTotalCount();

        //封装PageBean对象
        PageBean<Count> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();

        return pageBean;
    }

    @Override
    public List<Count> toSexCountAvg() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> counts = mapper.toSexCountAvg();
        sqlSession.commit();
        sqlSession.close();
        return counts;
    }

    @Override
    public List<Count> toCourseAvg() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> counts = mapper.toCourseAvg();
        sqlSession.close();

        return counts;
    }

    @Override
    public List<Count> studentCourse() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> counts = mapper.studentCourse();
        sqlSession.close();
        return counts;
    }

    @Override
    public List<Count> demo() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> demo = mapper.demo();
        sqlSession.commit();
        sqlSession.close();
        return demo;
    }

    @Override
    public List<Count> countOriginPlace() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> counts = mapper.countOriginPlace();
        sqlSession.commit();
        sqlSession.close();
        return counts;
    }

    @Override
    public List<Count> countFile() {
        SqlSession sqlSession = factory.openSession();
        CountMapper mapper = sqlSession.getMapper(CountMapper.class);
        List<Count> counts = mapper.countFile();
        sqlSession.commit();
        sqlSession.close();
        return counts;

    }


}
